<!-- 实名认证 -->
<template>
	<view>
		<view class="carc-view">
			<view class="row-between">
				<text class="cell-text">真实姓名</text>
				<input class="cell-tip" type="text" v-model="name" placeholder-class="inputHolder" placeholder="请填写真实姓名" />
			</view>
			<view class="row-between">
				<text class="cell-text">身份证号</text>
				<input class="cell-tip" type="text" v-model="idCard" placeholder-class="inputHolder" placeholder="请填写身份证号" />
			</view>
			<picker mode="multiSelector" @columnchange="bindMultiPickerColumnChange" @change="bindPickerChange" :value="multiIndex"
			 :range="[provinceArr, cityArr, areaArr]">
				<view class="row-between">
					<text class="cell-text">身份证地址</text>
					<view class="row-div">
						<text class="cell-tip" :class="{unSet: !textArr.length}">{{ textArr.join('-') || '请选择城市'}}</text>
						<uni-icons type="arrowright" style="font-size: 28upx;" :style="{color: !textArr.length ? '#999999' : '#333333'}"></uni-icons>
					</view>
				</view>
			</picker>
		</view>
		<!-- 保存 -->
		<view class="column-div">
			<view @click="realNameHandler" class="column-div save-btn">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	import navHeader from '@/components/nav-header/nav-header';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		components: {
			navHeader
		},
		data() {
			return {
				icon: {
					backBtn: "/static/images/common/return.png",
					rDefaultBtn: "/static/images/user/userInfo/r_default.png",
					rSelectBtn: "/static/images/user/userInfo/r_select.png",
				},
				name: "",
				idCard: "",
				address: [
					[],
					[],
					[]
				],
				multiIndex: [0, 0, 0],
				codeArr: [],
				textArr: [],
				provinceArr: [],
				cityArr: [],
				areaArr: [],
				formProvince: null
			};
		},
		computed: {
			...mapState(['userInfo']),
		},
		onLoad(options) {
			console.log("userInfo", userInfo)
			let userInfo = JSON.parse(options.userInfo) || {};
			this.name = userInfo['name'] || "";
			this.idCard = userInfo['fullIdCard'] || ""
			if (userInfo.provinceCode && userInfo.cityCode && userInfo.districtCode) {
				this.codeArr = [userInfo.provinceCode, userInfo.cityCode, userInfo.districtCode]
			}
			if (userInfo.province && userInfo.city && userInfo.district) {
				this.textArr = [userInfo.province, userInfo.city, userInfo.district]
			}
			this.getProvince(86, 0);
		},
		methods: {
			bindPickerChange(e) {
				let arr = e.target.value || []
				this.codeArr = []
				this.textArr = []
				arr.forEach((item, index) => {
					this.codeArr.push(this.address[index][item].value)
					this.textArr.push(this.address[index][item].text)
				})
			},
			//实名认证
			realNameHandler() {
				let that = this;
				if (!this.name) return this.$api.msg("请输入真实姓名");
				if (!this.idCard) return this.$api.msg("请输入身份证号");
				if (!this.codeArr.length) return this.$api.msg("请选择城市");
				let codeArr = this.codeArr
				let textArr = this.textArr
				uni.showLoading();
				that.$api.globalUtil.requestAjax({
					url: "/CustomerApi/checkCustomerIdCardForApi",
					method: "POST",
					data: {
						name: this.name,
						idCard: this.idCard,
						province: textArr[0],
						provinceCode: codeArr[0],
						city: textArr[1],
						cityCode: codeArr[1],
						district: textArr[2],
						districtCode: codeArr[2],
					},
					errorMsg: "实名认证未通过",
					success: function(res) {
						if (res.data.success) {
							that.$api.msg("认证成功");
							setTimeout(function() {
								uni.hideLoading();
								uni.navigateBack();
							}, 1500);
						} else {
							uni.hideLoading();
							that.$api.msg(res.data.msg || "认证失败");
						}
					}
				});
			},
			// 获得省份
			getProvince(id, index) {
				return this.$api.globalUtil.requestAjax({
					url: '/AreaApi/listForApi',
					method: 'POST',
					data: {
						grade: index + 1,
						parentId: id
					},
					success: (res) => {
						// console.log(res)
						let keyArr = ['provinceArr', 'cityArr', 'areaArr']
						let arr = res.data.response
						this.address[index] = arr
						let list = []
						arr.forEach(item => list.push(item.text))
						this[keyArr[index]] = list
						if (id == 86 && index == 0 && list.length) {
							this.getProvince(arr[0].value, 1).then(res => index != 2 ? this.getProvince(this.address[1][0].value, 2) :
								null)
						}
					}
				});
			},

			// 三级联动
			bindMultiPickerColumnChange(e) {
				let [column, index] = [e.detail.column, e.detail.value]
				this.multiIndex[column] = index
				console.log(column, index)
				if (column == 0) {
					this.multiIndex[1] = 0
					this.multiIndex[2] = 0
				}
				if (column == 1) {
					this.multiIndex[2] = 0
				}
				if (column == 2) return;
				this.getProvince(this.address[column][index].value, column + 1).then(res => this.getProvince(this.address[1][this.multiIndex[1]].value, column + 2))
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F5F5F5;
	}


	// header部分
	.header-Container {
		position: relative;
		width: 100%;
		height: 176rpx;
		overflow: hidden;
		box-shadow: 0px 1px 1px 0px rgba(0, 0, 0, 0.08);
		background: rgba(255, 255, 255, 1);

		.leftBtns {
			display: flex;
			align-items: center;
			justify-content: flex-start;
			position: absolute;
			bottom: 0;
			width: 100%;
			height: 86rpx;

			.back {
				margin-left: 17rpx;
				width: 44rpx;
				height: 44rpx;
			}
		}

		.title {
			position: absolute;
			bottom: 0;
			width: 100%;
			height: 86rpx;
			line-height: 86rpx;
			text-align: center;
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: rgba(51, 51, 51, 1);
		}
	}

	.carc-view {
		width: 690upx;
		margin: 20upx auto;
		background: #FFFFFF;
		border-radius: 10upx;
		margin-bottom: 140upx;
		padding: 0 45upx;

		.row-between {
			width: 100%;
			padding: 40upx 0;
			border-bottom: 1px solid #EEEEEE;

			&:last-child {
				border: none;
			}

			.cell-text {
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
			}

			.cell-tip {
				text-align: right;
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #333333;
			}

			.inputHolder {
				text-align: right;
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
			}

			.unSet {
				color: #999999;
			}
		}
	}

		.save-btn {
		width: 600upx;
		height: 90upx;
		background: #FE5E49;
		border-radius: 45upx;
		font-size: 34upx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}
</style>
